<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="75px"
    >
      <!-- <el-form-item label="关联支付ID" prop="paymentId">
        <el-input
          v-model="queryParams.paymentId"
          placeholder="请输入关联支付ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="订单编号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="退款金额" prop="refundAmt">
        <el-input
          v-model="queryParams.refundAmt"
          placeholder="请输入退款金额"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="退款状态" prop="refundStatus">
        <el-select
          v-model="queryParams.refundStatus"
          placeholder="请选择退款状态"
          clearable
        >
          <el-option
            v-for="dict in refund_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="退款原因" prop="refundReason">
        <el-select v-model="queryParams.refundReason" placeholder="请选择退款原因" clearable>
          <el-option
            v-for="dict in refund_reason"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item
        label="微信退款交易单号"
        label-width="130"
        prop="refundTransId"
      >
        <el-input
          v-model="queryParams.refundTransId"
          placeholder="请输入微信退款交易单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退款时间" prop="refundTime">
        <el-date-picker
          clearable
          v-model="queryParams.refundTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择退款时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
        >
          <el-option
            v-for="dict in wdm_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="版本号" prop="version">
        <el-input
          v-model="queryParams.version"
          placeholder="请输入版本号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="退款原因" prop="reason">
        <el-select
          v-model="queryParams.reason"
          placeholder="请选择退款原因"
          clearable
        >
          <el-option
            v-for="dict in refund_reason"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="退款单号" prop="refundCode">
        <el-input
          v-model="queryParams.refundCode"
          placeholder="请输入退款单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['ruoyi:refundInfo:add']"
        >新增</el-button>
      </el-col> -->

      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ruoyi:refundInfo:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ruoyi:refundInfo:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['ruoyi:refundInfo:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="refundInfoList"
      border
      resizble
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="序号"
        type="index"
        width="60"
        align="center"
        prop="refundId"
      />
      <!-- <el-table-column label="关联支付ID" align="center" prop="paymentId" /> -->
      <el-table-column
        label="退款单号"
        align="center"
        min-width="240"
        prop="refundCode"
      />
      <el-table-column label="订单编号" align="center" prop="orderId" />

      <!-- <el-table-column label="退款金额" align="center" prop="refundAmt" /> -->
      <el-table-column prop="refundAmt" align="center" label="退款金额">
        <template #default="scope">
          {{ formatAmount(scope.row.refundAmt) }}
        </template>
      </el-table-column>

      <el-table-column label="退款状态" align="center" prop="refundStatus">
        <template #default="scope">
          <dict-tag :options="refund_status" :value="scope.row.refundStatus" />
        </template>
      </el-table-column>
      <el-table-column
        label="退款原因"
        align="center"
        min-width="150"
        prop="reason"
      >
        <template #default="scope">
          <dict-tag :options="refund_reason" :value="scope.row.reason" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="退款原因" align="center" prop="refundReason">
        <template #default="scope">
          <dict-tag :options="refund_reason" :value="scope.row.refundReason"/>
        </template>
      </el-table-column> -->
      <!-- <el-table-column
        label="微信退款交易单号"
        align="center"
        min-width="280"
        prop="refundTransId"
      /> -->
      <el-table-column
        label="退款时间"
        align="center"
        prop="refundTime"
        width="240"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.refundTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="wdm_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="版本号" align="center" prop="version" /> -->
      <!-- <el-table-column label="备注信息" align="center" prop="remark" /> -->

      <el-table-column
        label="操作"
        align="center"
        min-width="150"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <!-- <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['ruoyi:refundInfo:edit']">修改</el-button> -->
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ruoyi:refundInfo:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改退款信息日志管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form
        ref="refundInfoRef"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="关联支付ID" prop="paymentId">
          <el-input v-model="form.paymentId" placeholder="请输入关联支付ID" />
        </el-form-item>
        <el-form-item label="关联订单ID" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入关联订单ID" />
        </el-form-item>

        <!-- <el-form-item label="退款金额" prop="refundAmt">
          <el-input v-model="form.refundAmt" placeholder="请输入退款金额" />
        </el-form-item> -->
        <el-form-item label="金额(元)">
          <el-input
            v-model="form.amount"
            placeholder="请输入金额"
            @input="handleAmountInput"
          >
            <template #append>元</template>
          </el-input>
        </el-form-item>

        <el-form-item label="退款状态" prop="refundStatus">
          <el-select v-model="form.refundStatus" placeholder="请选择退款状态">
            <el-option
              v-for="dict in refund_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="退款原因" prop="refundReason">
          <el-select v-model="form.refundReason" placeholder="请选择退款原因">
            <el-option
              v-for="dict in refund_reason"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="微信退款交易单号" prop="refundTransId">
          <el-input
            v-model="form.refundTransId"
            placeholder="请输入微信退款交易单号"
          />
        </el-form-item>
        <el-form-item label="退款成功时间" prop="refundTime">
          <el-date-picker
            clearable
            v-model="form.refundTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择退款成功时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in wdm_status"
              :key="dict.value"
              :label="dict.value"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-radio-group v-model="form.delFlag">
            <el-radio
              v-for="dict in del_flag"
              :key="dict.value"
              :label="dict.value"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="版本号" prop="version">
          <el-input v-model="form.version" placeholder="请输入版本号" />
        </el-form-item>
        <el-form-item label="备注信息" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="退款原因" prop="reason">
          <el-select v-model="form.reason" placeholder="请选择退款原因">
            <el-option
              v-for="dict in refund_reason"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="退款单号" prop="refundCode">
          <el-input v-model="form.refundCode" placeholder="请输入退款单号" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="RefundInfo">
import {
  listRefundInfo,
  getRefundInfo,
  delRefundInfo,
  addRefundInfo,
  updateRefundInfo,
} from "@/api/ruoyi/refundInfo";

const { proxy } = getCurrentInstance();
const { refund_status, wdm_status, refund_reason, del_flag } = proxy.useDict(
  "refund_status",
  "wdm_status",
  "refund_reason",
  "del_flag"
);

const refundInfoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    paymentId: null,
    orderId: null,
    refundAmt: null,
    refundStatus: null,
    refundReason: null,
    refundTransId: null,
    refundTime: null,
    status: null,
    version: null,
    reason: null,
    refundCode: null,
  },
  rules: {
    paymentId: [
      { required: true, message: "关联支付ID不能为空", trigger: "blur" },
    ],
    orderId: [
      { required: true, message: "关联订单ID不能为空", trigger: "blur" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询退款信息日志管理列表 */
function getList() {
  loading.value = true;
  listRefundInfo(queryParams.value).then((response) => {
    // refundInfoList.value = response.rows
    // 过滤已删除数据（假设 delFlag 字段标识删除状态）
    refundInfoList.value = response.rows.filter((item) => item.delFlag !== "2");
    total.value = response.total;
    loading.value = false;
  });
}

// 格式化金额（分转元）
function formatAmount(value) {
  if (!value && value !== 0) return "--";
  return (value / 100).toFixed(2);
}

// 输入处理（元转分）
function handleAmountInput(value) {
  // 移除非数字和小数点字符
  const numericValue = value.replace(/[^0-9.]/g, "");

  // 分割整数和小数部分
  const parts = numericValue.split(".");

  // 保留两位小数
  if (parts[1] && parts[1].length > 2) {
    parts[1] = parts[1].slice(0, 2);
  }

  // 组合并转换为分
  const formattedValue = parts[0] + (parts[1] ? `.${parts[1]}` : "");
  const cents = Math.round(parseFloat(formattedValue || "0") * 100);

  // 更新表单值（存储为分）
  form.value.amount = cents;
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    refundId: null,
    paymentId: null,
    orderId: null,
    refundAmt: null,
    refundStatus: null,
    refundReason: null,
    refundTransId: null,
    refundTime: null,
    status: null,
    delFlag: null,
    version: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    remark: null,
    reason: null,
    refundCode: null,
  };
  proxy.resetForm("refundInfoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.refundId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加退款信息日志管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _refundId = row.refundId || ids.value;
  getRefundInfo(_refundId).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改退款信息日志管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["refundInfoRef"].validate((valid) => {
    if (valid) {
      if (form.value.refundId != null) {
        updateRefundInfo(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addRefundInfo(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _refundIds = row.refundId || ids.value;
  proxy.$modal
    .confirm('是否确认删除退款信息日志管理编号为"' + _refundIds + '"的数据项？')
    .then(function () {
      return delRefundInfo(_refundIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "ruoyi/refundInfo/export",
    {
      ...queryParams.value,
    },
    `refundInfo_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>

<style scoped>
/* 金额显示样式 */
.amount-display {
  font-family: monospace;
  color: #1890ff;
}
</style>
